package com.itheima.health.entity;

import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 定时任务实现定时清理垃圾图片1
 *
 * @author admin
 * @since 2023/4/27 19:22
 */
@Slf4j
@Component
public class ClearImg {

    @Autowired
    RedisTemplate redisTemplate;




    @Autowired
    QiniuUtils qiniuUtils;

    @Scheduled(cron = "0 */1 * * * ? ")
    public void clearImg(){
        //根据set集合求出两个集合的差集
        final Set<String> difference = redisTemplate.opsForSet().difference(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DBRESOURCES);

        if (difference != null){
            for (String lajiImg : difference) {
                //删除七牛云上的垃圾图片
                qiniuUtils.deleteFileFromQiniu(lajiImg);
                //删除Redis集合中垃圾图片
                final SetOperations setOperations = redisTemplate.opsForSet();
                setOperations.remove(RedisConstant.SETMEAL_PIC_RESOURCES,lajiImg);
                log.info("清理垃圾图片{}",lajiImg);
            }
        }
    }
}
